/**
 * Copyright 2024 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

# tfdoc:file:description GCVE private cloud resources.

locals {
  network_peerings = {
    for k, v in var.network_peerings : k => merge(v, {
      # interpolate FAST VPC ids if available
      peer_network = lookup(var.vpc_self_links, v.peer_network, v.peer_network)
    })
  }
}

module "gcve-pc" {
  source     = "../../../modules/gcve-private-cloud"
  prefix     = var.prefix
  project_id = module.gcve-project-0.id
  vmw_network_config = {
    create = true
    name   = "default"
  }
  vmw_network_peerings      = local.network_peerings
  vmw_private_cloud_configs = var.private_cloud_configs
}

# optional reverse peering configuration from the peer network projects

resource "google_vmwareengine_network_peering" "vmw_engine_network_peerings" {
  for_each = {
    for k, v in local.network_peerings : k => v if v.configure_peer_network
  }
  project = regex(
    "projects/([^/]+)/", each.value.peer_network
  )[0]
  name                                = "${var.prefix}-${each.key}"
  description                         = each.value.description
  peer_network                        = each.value.peer_network
  peer_network_type                   = "STANDARD"
  vmware_engine_network               = module.gcve-pc.network_id
  export_custom_routes                = each.value.routes_config.import
  export_custom_routes_with_public_ip = each.value.routes_config.public_import
  import_custom_routes                = each.value.routes_config.export
  import_custom_routes_with_public_ip = each.value.routes_config.public_export
}
